package com.koron.standard.jobStock.experiment.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.koron.standard.jobStock.experiment.bean.dto.ExperimentItemDTO;
import com.koron.standard.jobStock.experiment.bean.po.ExperimentItem;
import com.koron.standard.jobStock.experiment.bean.po.ExperimentItemParams;
import com.koron.standard.jobStock.experiment.mapper.ExperimentItemMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 试验标准下工作项目(ExperimentItem)表服务实现类
 *
 * @author jzp
 * @since 2021-09-16
 */
@Service
public class ExperimentItemService extends ServiceImpl<ExperimentItemMapper, ExperimentItem> {

    @Autowired
    private ExperimentItemParamsService experimentItemParamsService;

    public List<ExperimentItemDTO> getDtoByExperimentId(String experimentId) {
        return getBaseMapper().selectDtoListByExperimentId(experimentId);
    }

    public List<ExperimentItemDTO> getTreeByExperimentId(String experimentId) {
        return toTree(getDtoByExperimentId(experimentId));
    }

    /**
     * DTO > tree
     */
    public List<ExperimentItemDTO> toTree(List<ExperimentItemDTO> itemDtos) {
        List<ExperimentItemDTO> itemDtoRoots = itemDtos.stream().filter(dto -> "0".equals(dto.getParentId())).collect(Collectors.toList());
        for (ExperimentItemDTO itemDtoRoot : itemDtoRoots) {
            List<ExperimentItemDTO> children = new ArrayList<>();
            for (int i = 0; i < itemDtos.size(); i++) {
                if (itemDtoRoot.getId().equals(itemDtos.get(i).getParentId())) {
                    children.add(itemDtos.get(i));
                    itemDtos.remove(i);
                    i = i - 1;
                }
            }
            itemDtoRoot.setChildren(children);
        }
        return itemDtoRoots;
    }

    public void saveRootDto(ExperimentItemDTO experimentItemDTO) {
        saveDto(experimentItemDTO, "0");
    }

    public void saveDto(ExperimentItemDTO experimentItemDTO, String parentId) {

        // 1. 保存工作项目
        experimentItemDTO.setParentId(parentId);
        save(experimentItemDTO);

        String experimentId = experimentItemDTO.getExperimentId();
        String experimentItemId = experimentItemDTO.getId();

        // 2. 保存记录项
        List<ExperimentItemParams> experimentItemParams = experimentItemDTO.getItemParams();
        // 设置作业标准id和作业标准项id
        for (ExperimentItemParams experimentItemParam : experimentItemParams) {
            experimentItemParam.setExperimentId(experimentId);
            experimentItemParam.setItemId(experimentItemId);
        }
        experimentItemParamsService.saveBatch(experimentItemParams);
        List<ExperimentItemDTO> experimentItemDTOS = Optional.ofNullable(experimentItemDTO.getChildren()).orElse(new ArrayList<>());

        // 3. 保存子节点工作项目
        for (ExperimentItemDTO child : experimentItemDTOS) {
            child.setExperimentId(experimentId);
            saveDto(child, experimentItemId);
        }

    }

    public void removeByExperimentId(String experimentId) {
        if (StrUtil.isEmpty(experimentId)) {
            return;
        }
        LambdaQueryWrapper<ExperimentItem> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ExperimentItem::getExperimentId, experimentId);
        List<ExperimentItem> list = list(wrapper);
        removeByIds(list.stream().map(ExperimentItem::getId).collect(Collectors.toList()));
        experimentItemParamsService.removeByExperimentId(experimentId);
    }
}
